package en.edu.zxj.springrabbimqdemo.controller;

import en.edu.zxj.springrabbimqdemo.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-09-20
 * @time: 13:55:01
 */
@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {
        // 发送消息
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE_NAME, "springboot: work 模式消息" + i);
        }
        return "work 发送消息成功";
    }


    @RequestMapping("/fanout")
    public String fanout() {
        // 发送消息
        // 无论 routingKey 为啥, 表⽰所有队列都可以收到消息
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE_NAME, "", "springboot: fanout 模式消息");
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE_NAME, "abc", "springboot: fanout 模式消息 abc");
        return "fanout 发送消息成功";
    }

    @RequestMapping("/direct")
    public String direct(String routingKey) {
        // 发送消息
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE_NAME, routingKey, "springboot: direct 模式消息 " + routingKey);
        return "direct 发送消息成功";
    }


    @RequestMapping("/topic")
    public String topic(String routingKey) {
        // 发送消息
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE_NAME, routingKey, "springboot: topic 模式消息 " + routingKey);
        return "topic 发送消息成功";
    }
}
